package cn.kgc.crowdfundingadmin.controller;

import cn.kgc.crowdfundingadmin.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin.pojo.Admin;
import cn.kgc.crowdfundingadmin.service.Adminservice;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class AdminController {
    @Resource
    Adminservice adminservice;


    @RequestMapping("/mahaibo")
    @ResponseBody
    public Admin getByid(Integer id){
      Admin admin=adminservice.selectByid(id);
      return  admin;

    }
//判断登录成和失败
@RequestMapping("/admin/do/login")
    public  String  doLogin(@RequestParam("loginAcct") String loginAcct,
                            @RequestParam("userPswd") String userPswd,
                            Model model,
                            HttpSession session){
Admin admin=adminservice.login(loginAcct,userPswd);
if (admin!=null){//登陆成功
    session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,admin);
    return  "redirect:/admin/to/main/page.html";

}else {//登录失败
    model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
    return "admin-login";
}
}


//分页查询
    @RequestMapping("/admin/query/for/search")
    public String queryForSearch(  @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
                                   @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
                                   @RequestParam(value="keyword", defaultValue="") String keyword,
                                   Model model){
        PageInfo<Admin> pageInfo = adminservice.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        System.out.println(pageInfo.toString());
        return "admin-page";

    }

    //局部异常处理
    public String catchException(Exception exception,Model model){
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE,exception);
        return "system-error";
    }

   //退出
@RequestMapping("/admin/logout")
    public  String logout(HttpSession session){
        session.invalidate();
    return "redirect:/index";

}


    //新增
    @RequestMapping("/admin/save")
    public String saveAdmin(Admin admin) {
        adminservice.saveAdmin(admin);
        return "redirect:/admin/query/for/search";
    }

    //打开修改页面
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        Admin admin = adminservice.getAdminById(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);
        return "admin-edit";
    }


    //修改
    @RequestMapping("/admin/update")
    public String updateAdmin(Admin admin,Integer pageNum) {
        try {
            //修改用户
            adminservice.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //保存当前页面 跳转页面
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }

}
